import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:flutter_syt/widgets/common_button.dart';

import '/theme/theme_color.dart';

class DialogEntity {
  String title;
  Widget contentWidget;
  EdgeInsets contentPadding;
  Function onCancel;
  Function onOk;
  String cancelText;
  String okText;
  bool disableAndriodPop;

  DialogEntity(
    this.title,
    this.contentWidget,
    this.onCancel,
    this.onOk, {
    this.disableAndriodPop = false,
    this.cancelText = "取消",
    this.okText = "确定",
    this.contentPadding = const EdgeInsets.only(bottom: 20),
  });

  String getSaveString() {
    if (okText == S().Done) {
      return S().Done;
    }
    return okText;
  }

  String getCancelString() {
    if (cancelText == S().Cancel) {
      return S().cancel;
    }
    return cancelText;
  }
}

class CustomDialog extends Dialog {
  Future<bool> _pop() async {
    print('物理返回了');
    if (entity.disableAndriodPop) {
      return false;
    }
    return true;
  }

  final DialogEntity entity;
  final bool hideCanel;

  CustomDialog(this.entity, {this.hideCanel = false, Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    // final mediaQueryData = MediaQuery.of(context);
    return WillPopScope(
        onWillPop: _pop,
        child: Padding(
          padding: const EdgeInsets.all(40.0),
          child: Material(
            type: MaterialType.transparency,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Container(
                  // margin: EdgeInsets.symmetric(horizontal: 50),
                  decoration: const ShapeDecoration(
                    color: ThemeColor.bkColorFFFFFF,
                    // color: Colors.red,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(
                        Radius.circular(16.0),
                      ),
                    ),
                  ),
                  padding: EdgeInsets.all(20.w),
                  child: Column(
                    children: [
                      if (this.entity.title.isNotEmpty)
                        // Padding(
                        //     padding: const EdgeInsets.fromLTRB(0, 0, 0, 0.0),
                        //     child: Center(
                        //       child:
                        Text(
                          entity.title,
                          textAlign: TextAlign.center,
                          style: TextStyle(
                              fontSize: 16.0, fontWeight: FontWeight.bold),
                        ),
                      // )),
                      Padding(
                        // padding: EdgeInsets.symmetric(
                        //   horizontal: 15,
                        //   vertical: 20
                        // ),
                        padding: entity.contentPadding,
                        child: entity.contentWidget,
                      ),
                      // Padding(
                      // padding: EdgeInsets.symmetric(horizontal: 20.w),
                      // child:
                      Row(
                        // mainAxisAlignment: MainAxisAlignment.spaceAround,
                        // crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Visibility(
                            visible: hideCanel == false,
                            child: Expanded(child: CommonButton.expend(
                              title: entity.getCancelString(),
                              isLine: true,
                              onClick: () {
                                entity.onCancel();
                              },
                            )),
                            // child: Expanded(
                            //     child: Container(
                            //   // width: 120.w,
                            //   // color: Colors.red,
                            //   height: 42,
                            //   child: OutlinedButton(
                            //     child: Text(this.entity.getCancelString()),
                            //     style: OutlinedButton.styleFrom(
                            //       // primary: ThemeColor.colorBlack80,
                            //       side: BorderSide(
                            //         color: ThemeColor.colorBlack40,
                            //         width: 0.5,
                            //       ),
                            //       shape: const RoundedRectangleBorder(
                            //         borderRadius:
                            //             BorderRadius.all(Radius.circular(8)),
                            //       ),
                            //       textStyle: TextStyle(
                            //         fontSize: 16,
                            //         // fontWeight: FontWeight.w300,
                            //       ),
                            //     ),
                            //     onPressed: ClickUtils.debounce(() {
                            //       this.entity.onCancel();
                            //     }),
                            //   ),

                            // ))
                          ),
                          Visibility(
                              visible: hideCanel == false,
                              child: const SizedBox(
                                width: 10,
                              )),
                          Expanded(
                            child: CommonButton.expend(
                              title: entity.getSaveString(),
                              onClick: () {
                                entity.onOk();
                              },
                            ),
                          ),
                          //   child: Container(
                          //     // width: 120.w,
                          //
                          //     height: 42,
                          //     child: OutlinedButton(
                          //       child: Text(
                          //         this.entity.getSaveString(),
                          //         maxLines: 1,
                          //       ),
                          //       style: OutlinedButton.styleFrom(
                          //         // primary: ThemeColor.colorBlack100,
                          //         backgroundColor: ThemeColor.primaryColor,
                          //         side: BorderSide(
                          //           color: ThemeColor.primaryColor,
                          //           width: 0.5,
                          //         ),
                          //         shape: const RoundedRectangleBorder(
                          //           borderRadius:
                          //               BorderRadius.all(Radius.circular(8)),
                          //         ),
                          //         textStyle: TextStyle(
                          //           fontSize: 16,
                          //           // fontWeight: FontWeight.w300,
                          //         ),
                          //       ),
                          //       onPressed: ClickUtils.debounce(() {
                          //         this.entity.onOk();
                          //       }),
                          //     ),
                          //   ),
                          // )
                        ],
                      ),
                      // )
                    ],
                  ),
                )
              ],
            ),
          ),
        ));
  }
}

class CustomSingleDialog extends Dialog {
  final DialogEntity entity;

  CustomSingleDialog(this.entity, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // final mediaQueryData = MediaQuery.of(context);
    return Padding(
      padding: const EdgeInsets.all(40.0),
      child: Material(
        type: MaterialType.transparency,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              decoration: ShapeDecoration(
                color: ThemeColor.bkColorFFFFFF,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(
                    Radius.circular(16.0),
                  ),
                ),
              ),
              padding: const EdgeInsets.fromLTRB(0, 20, 0, 20),
              child: Column(
                children: [
                  if (this.entity.title.isNotEmpty)
                    Padding(
                      padding: const EdgeInsets.fromLTRB(0, 0, 0, 0.0),
                      child: Center(
                        child: Text(
                          this.entity.title,
                          style: TextStyle(
                              fontSize: 20.0,
                              color: Colors.black,
                              fontWeight: FontWeight.bold),
                        ),
                      ),
                    ),
                  Padding(
                    // padding: EdgeInsets.symmetric(vertical: 20,horizontal: 15),
                    padding: this.entity.contentPadding,
                    child: this.entity.contentWidget,
                  ),
                  Padding(
                      padding: EdgeInsets.symmetric(horizontal: 15),
                      child: Container(
                        height: 42,
                        child: OutlinedButton(
                          child: Text(this.entity.getSaveString()),
                          style: OutlinedButton.styleFrom(
                            // primary: ThemeColor.colorBlack100,
                            backgroundColor: ThemeColor.primaryColor,
                            side: BorderSide(
                              color: ThemeColor.primaryColor,
                              width: 1,
                            ),
                            shape: const RoundedRectangleBorder(
                              borderRadius:
                                  BorderRadius.all(Radius.circular(8)),
                            ),
                            textStyle: TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.w400,
                            ),
                          ),
                          onPressed: ClickUtils.debounce(() {
                            this.entity.onOk();
                          }),
                        ),
                      )
                      // ),
                      ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
